<?php

namespace App\Listeners;

use App\Common\Constant\Params;
use App\Events\MasterOrderRefund;
use App\Events\MerchantOrderRefund;
use App\Events\OrderRefunded;
use App\Models\OrderRefund;
use App\Notifications\OrderNotifications\MerchantOrderRefundNotification;
use App\Notifications\OrderNotifications\OrderRefundNotification;
use App\Services\ISystemService;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class MerchantOrderRefundListener implements ShouldQueue
{

    use InteractsWithQueue;

    public $queue = 'finance';

    public $tries = 3;

    public $timeout = 30;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  CacheHit  $event
     * @return void
     */
    public function handle(MerchantOrderRefund $event)
    {
        $refund = OrderRefund::find($event->refund_id);
        if(!$refund){
            throw new \Exception('miss refund');
        }

        $refund->order->master->notify(new MerchantOrderRefundNotification($refund));
    }

    public function failed(MerchantOrderRefund $event, \Exception $exception)
    {
        //
    }
}
